﻿@using Microsoft.AspNetCore.Html
@using SIG.Data.Entity.Identity
@using SIG.Data.Enums
@using SIG.Infrastructure.Configs
@using TZGCMS.Model.Admin.ViewModel.Identity
@model SetRoleMenusVM

@*@{
    Func<IEnumerable<Menu>, IHtmlContent> ShowTree = @<text>@{
        var menus = item;
                                                                <ul class="menuTree menuTreeRole list-unstyled">
                                                                @foreach (var m in menus)
                                                                {
                                                                    if ((m.MenuType == MenuType.PAGE && m.LayoutLevel <2 ) || m.MenuType == MenuType.NOLINK)
                                                                    {
                                                                        if (m.Active)
                                                                        {
                                                                            <li class="@(m.ChildMenus.Any() ? "hasMenus" : "")">
                                                                                <label>

                                                                                    <input type="checkbox" name="menuId" value="@m.Id" @(Model.MenuIds.Contains(m.Id) ? "checked" : "") />
                                                                                    @m.Title
                                                                                </label>
                                                                                @if (m.ChildMenus.Any())
                                                                                {
                                                                                    //ShowTree(m.ChildMenus.OrderBy(c => c.Importance));
                                                                                }
                                                                            </li>
                                                                        }

                                                                    }
                                                                    else
                                                                    {
                                                                        <li class="@(m.ChildMenus.Any() ? "hasMenus" : "")">
                                                                            <label>

                                                                                <input type="checkbox" name="menuId" value="@m.Id" @(Model.MenuIds.Contains(m.Id) ? "checked" : "") />
                                                                                @m.Title
                                                                            </label>
                                                                            @if (m.ChildMenus.Any())
                                                                            {
                                                                                //ShowTree(m.ChildMenus.OrderBy(c => c.Importance));
                                                                            }
                                                                        </li>
                                                                    }
                                                                }
                                                                  
                                        </ul>
    }</text>;
}

@ShowTree(Model.Menus.Where(m => m.ParentId == null))*@


@*@helper ShowTree(IEnumerable<Menu> menus)
{

    <ul class="menuTree menuTreeRole list-unstyled">
        @foreach (var item in menus)
        {
            if ((item.MenuType == MenuType.PAGE && item.LayoutLevel <2 ) || item.MenuType == MenuType.NOLINK)
            {
                if (item.Active)
                {
                    <li class="@(item.ChildMenus.Any() ? "hasMenus" : "")">
                        <label>

                            <input type="checkbox" name="menuId" value="@item.Id" @(Model.MenuIds.Contains(item.Id) ? "checked" : "") />
                            @item.Title
                        </label>
                        @if (item.ChildMenus.Any())
                        {
                            @ShowTree(item.ChildMenus.OrderBy(c => c.Importance))
                        }
                    </li>
                }

            }
            else
            {
                <li class="@(item.ChildMenus.Any() ? "hasMenus" : "")">
                    <label>

                        <input type="checkbox" name="menuId" value="@item.Id" @(Model.MenuIds.Contains(item.Id) ? "checked" : "") />
                        @item.Title
                    </label>
                    @if (item.ChildMenus.Any())
                    {
                        @ShowTree(item.ChildMenus.OrderBy(c => c.Importance))
                    }
                </li>
            }

        }
    </ul>
}*@

@*@using (Ajax.BeginForm("SetRoleMenus", "Role",
        new AjaxOptions()
        {
            HttpMethod = "POST",
            OnBegin = "onBeginSave",
            OnComplete = "onCompleteSave",
            OnSuccess = "updateSuccessRoleMenus",
            //OnFailure = "handleError",
            //  UpdateTargetId = "RoleList"

        }, new { @class = "form-horizontal", role = "form" }))
{*@

<form asp-controller="Role" asp-action="SetRoleMenus" data-ajax-begin="onBeginSave" data-ajax-complete="onCompleteSave"
      data-ajax-failure="onFailed" data-ajax-success="onSuccessSave"
      data-ajax="true" data-ajax-method="POST">

@Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.RoleId)

    <!-- 添加菜单 Modal -->
    <div class="modal fade" id="ModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="myModalLabel">权限设置</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                   
                </div>
                <div class="modal-body" style="height:450px;overflow-y:auto;padding-left:35px;">
                    @*@ShowTree(Model.Menus.Where(m=>m.ParentId == null))*@
                    @await Component.InvokeAsync("RoleMenus", new { id = Model.RoleId })
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button class="btn btn-primary" type="submit" id="btnSave"><i class="fa fa-save fa-fw"></i>保存</button>
                </div>
            </div>
        </div>
    </div>
</form>